define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/middleman/index',
                    add_url: 'auth/middleman/add',
                    edit_url: 'auth/middleman/edit',
                    // del_url: 'auth/middleman/del',
                    multi_url: 'auth/middleman/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'phone', title: '手机', operate: 'LIKE'},
                        {field: 'user.avatar', title: '头像', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'real_name', title: '真实姓名', operate: 'LIKE'},
                        {field: 'card_no', title: '银行卡号', operate: 'LIKE'},
                        {field: 'bank_name', title: '开户行', operate: 'LIKE'},
                        {field: 'rebate_amt', title: '返点金额', operate: 'BETWEEN', sortable: true},
                        {field: 'admin.nickname', title: '所属销售', operate: 'LIKE'},
                        {field: 'create_dt', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                        buttons: [{
                            name: "item",
                            classname: "btn btn-xs btn-success btn-dialog",
                            icon: "fa fa-eye",
                            text: "关联账单",
                            url: "auth/middleman/item",
                        },
                        {
                            name: "edit",
                            text: __("详情"),
                            classname: "btn btn-xs btn-info btn-dialog",
                            icon: "fa fa-pencil",
                            url: "auth/middleman/edit"
                        },
                        // {
                        //     name: 'del',
                        //     text: '删除',
                        //     icon: 'fa fa-trash',
                        //     classname: 'btn btn-xs btn-danger btn-delone',
                        //     url: 'auth/middleman/del'
                        // },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        item: function () {
            var ids = $("#assign-data-ids").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'auth/middleman/item/ids/' + ids,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: '用户名', operate: 'LIKE'},
                        {field: 'shop.shop_name', title: '店铺名称', operate: 'LIKE'},
                        {field: 'shop.shop_type_text', title: '下单平台', operate: 'LIKE'},
                        {field: 'play.back_money_text', title: '优惠条件'},
                        {field: 'play.service_amt', title: '服务费'},
                        {field: 'play.scheme_no_text', title: '活动方案', operate: 'LIKE'},
                        {field: 'place_time', title: '抢单时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_status', title: __('Status'), searchList: {"1":"待上传","2":"待审核","3":"已完成","6":"待商家复审","-1":"已取消","-2":"超时取消","-3":"已拒绝",'-4':'财务取消','-5':'不满意反馈','-6':'不满意通过'}, formatter: Table.api.formatter.status},
                        {field: 'create_dt', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_dt', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});